package com.fdb.efp.riskm.api.rest;

import com.fdb.efp.riskm.api.service.RiskmUtilService;
import com.fdb.basic.framework.core.constant.*;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.framework.core.vo.UserInfo;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.loan.service.facade.AccLoanService;
import com.fdb.efp.loan.service.facade.LoanRepayDetailService;
import com.fdb.efp.loan.service.vo.AccLoanVO;
import com.fdb.efp.loan.service.vo.LoanRepayDetailVO;
import com.fdb.efp.riskm.common.RiskColltConstant;
import com.fdb.efp.riskm.service.facade.ColltResRecordService;
import com.fdb.efp.riskm.service.facade.ColltTaskCommonService;
import com.fdb.efp.riskm.service.facade.ColltTaskDistrAppService;
import com.fdb.efp.riskm.service.facade.ColltTaskInfoService;
import com.fdb.efp.riskm.service.vo.ColltResRecordVO;
import com.fdb.efp.riskm.service.vo.ColltTaskDistrAppVO;
import com.fdb.efp.riskm.service.vo.ColltTaskInfoVO;
import com.fdb.efp.riskm.service.vo.ColltTaskRcdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;


/**
 * Controller：催收任务信息
 *
 * @author ligm
 * @date 2019-08-27
 */
@RestController
@RequestMapping("/api")
public class ColltTaskInfoController extends AbstractController {

    @Autowired
    @Qualifier("colltTaskInfoService")
    private ColltTaskInfoService colltTaskInfoService;

    @Autowired
    @Qualifier("sRoleDataRuleService")
    private SRoleDataRuleService sRoleDataRuleService;

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;

    @Autowired
    @Qualifier("colltResRecordService")
    private ColltResRecordService colltResRecordService;

    @Autowired
    private RiskmUtilService riskmUtilService;

    @Autowired
    @Qualifier("colltTaskCommonService")
    private ColltTaskCommonService colltTaskCommonService;

    @Autowired
    @Qualifier("loanRepayDetailService")
    private LoanRepayDetailService loanRepayDetailService;

    @Autowired
    @Qualifier("colltTaskDistrAppService")
    private ColltTaskDistrAppService colltTaskDistrAppService;


    /**
     * 新增数据
     *
     * @param inColltTaskInfoVo
     * @return
     */
    @PostMapping(value = "/collt/task/info")
    public @ResponseBody
    ResponseData<Integer> insertColltTaskInfo(@RequestBody ColltTaskInfoVO inColltTaskInfoVo) {
        int outputVo = colltTaskInfoService.insertColltTaskInfo(inColltTaskInfoVo);
        return getResponseData(outputVo);
    }

    /**
     * 根据主键删除信息
     *
     * @param inColltTaskInfoVo
     * @return
     */
    @DeleteMapping(value = "/collt/task/info")
    public @ResponseBody
    ResponseData<Integer> deleteByPk(@RequestBody ColltTaskInfoVO inColltTaskInfoVo) {

        int outputVo = colltTaskInfoService.deleteByPk(inColltTaskInfoVo);
        return getResponseData(outputVo);
    }

    /**
     * 根据主键更新信息
     *
     * @param inColltTaskInfoVo
     * @return
     */
    @PutMapping(value = "/collt/task/info")
    public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody ColltTaskInfoVO inColltTaskInfoVo) {
        ColltTaskInfoVO colltTaskInfoVo = new ColltTaskInfoVO();
        colltTaskInfoVo.setColltTaskNo(inColltTaskInfoVo.getColltTaskNo());
        colltTaskInfoVo.setInputUserCode(inColltTaskInfoVo.getInputUserCode());
        colltTaskInfoVo.setInputUserName(inColltTaskInfoVo.getInputUserName());
        colltTaskInfoVo.setIdentWay(RiskColltConstant.IDENT_WAY_02);
        colltTaskInfoVo.setIsTaskDistr(BaseConstant.YES_Y);
        // 客户经理赋值为分配的用户编号
        colltTaskInfoVo.setCusManager(inColltTaskInfoVo.getInputUserCode());
        colltTaskInfoVo.setLastUpdateUser(inColltTaskInfoVo.getLastUpdateUser());
        int outputVo = colltTaskInfoService.updateByPk(colltTaskInfoVo);
        return getResponseData(outputVo);
    }

    /**
     * 根据主键查询详情
     *
     * @param
     * @return
     */
    @GetMapping(value = "/collt/task/info")
    public @ResponseBody
    ResponseData<ColltTaskInfoVO> queryByPk() {

        ColltTaskInfoVO inVo = new ColltTaskInfoVO();
        ColltTaskInfoVO outputVo = colltTaskInfoService.queryByPk(inVo);
        return getResponseData(outputVo);
    }

    /**
     * 列催收任务池列表数据查询
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskInfoAll(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        return getResponseData(outputVo);
    }

    @RequestMapping(value = "/collt/task/infos/props", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskInfoProps(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        outputVo = colltTaskInfoService.queryColltTaskInfoProps(vo);
        return getResponseData(outputVo);
    }

    /**
     * 电话催收任务管理列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/tel/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskTelInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        return getResponseData(outputVo);
    }

    /**
     * 专项催收任务管理列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/special/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskSpecialInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        return getResponseData(outputVo);
    }


    /**
     * 委外催收任务管理-受理页面列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/outs/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskOutsInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        riskmUtilService.displayOrgName(outputVo,"opOrgCode","opOrgName");
        riskmUtilService.displayUserName(outputVo,"opUserCode","opUserName");
        return getResponseData(outputVo);
    }


    /**
     * 诉讼催收任务管理-分配查看页面列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/law/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskLawInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
            logger.info("开始导出数据！");
            outputVo = expColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        } else {
            logger.info("开始查询数据！");
            outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        }
        return getResponseData(outputVo);
    }

    /**
     * 风险代理-分配查看页面列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/riskAgt/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryColltTaskRiskAgtInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
            logger.info("开始导出数据！");
            outputVo = expColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        } else {
            logger.info("开始查询数据！");
            outputVo = getColltTaskInfoByDataRuleType(vo, dataRuleTypeMap);
        }
        return getResponseData(outputVo);
    }

    /**
     * 列表查询
     *
     * @param vo
     * @param dataRuleTypeMap
     * @return
     */
    private List<ColltTaskInfoVO> getColltTaskInfoByDataRuleType(ColltTaskInfoVO vo, Map<String, String> dataRuleTypeMap) {
        List<ColltTaskInfoVO> outputVo = null;
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        // 根据权限方案调用不同的方法进行数据查询
        switch (dataRuleType) {
            case Owner:
                outputVo = colltTaskInfoService.queryAllOwner(vo);
                break;
            case CurrOrg:
                outputVo = colltTaskInfoService.queryAllCurrOrg(vo);
                break;
            case CurrDownOrg:
                outputVo = colltTaskInfoService.queryAllCurrDownOrg(vo);
                break;
            case CurrOwnerPrd:
                outputVo = colltTaskInfoService.queryAllCurrOwnerPrd(vo);
                break;
            default:
                outputVo = colltTaskInfoService.queryAllOwner(vo);
                break;
        }
        return outputVo;
    }

    /**
     * 数据导出
     *
     * @param vo
     * @param dataRuleTypeMap
     * @return
     */
    private List<ColltTaskInfoVO> expColltTaskInfoByDataRuleType(ColltTaskInfoVO vo, Map<String, String> dataRuleTypeMap) {
        List<ColltTaskInfoVO> outputVo = null;
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        // 根据权限方案调用不同的方案进行数据导出
        switch (dataRuleType) {
            case Owner:
                outputVo = colltTaskInfoService.queryExAllOwner(vo);
                break;
            case CurrOrg:
                outputVo = colltTaskInfoService.queryExAllCurrOrg(vo);
                break;
            case CurrDownOrg:
                outputVo = colltTaskInfoService.queryExAllCurrDownOrg(vo);
                break;
            case CurrOwnerPrd:
                outputVo = colltTaskInfoService.queryExAllCurrOwnerPrd(vo);
                break;
            default:
                outputVo = colltTaskInfoService.queryExAllOwner(vo);
                break;
        }
        return outputVo;
    }


    /**
     * 根据 催收方式=“专项催收” 和 催收员名称为空  查询全部数据
     *
     * @return
     * @author xiezw
     * @params * @param null
     * @date 2019/8/29 9:46
     * @version 0.1
     */
    @RequestMapping(value = "/collt/task/infos/all", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryByColltWay(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = colltTaskInfoService.queryByColltWayByPage(vo);
        return getResponseData(outputVo);
    }

    /**
     * 根据 催收方式=“电话催收” 和 催收员名称为空  查询全部数据
     *
     * @return
     * @author dengzc
     * @date 2019/8/30 9:29
     * @version 0.1
     */
    @RequestMapping(value = "/collt/task/infos/call", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryByCallWay(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = colltTaskInfoService.queryByCallWayByPage(vo);
        return getResponseData(outputVo);
    }

    /**
     * TODO: 任务认领 ：更新催收任务的是否已分配、催收员id、名称、最后修改人
     *
     * @return
     * @author xiezw
     * @params * @param null
     * @date 2019/9/2 16:10
     * @version 0.1
     */
    @PutMapping(value = "/collt/task/info/alw")
    public @ResponseBody
    ResponseData<Integer> updateByColltWay(@RequestBody ColltTaskInfoVO inColltTaskInfoVo) {
        UserInfo userInfo = new UserInfo();
        userInfo.getUserName();
        setUserInfoToVO(inColltTaskInfoVo);
        //  更新是否已分配为"是"
        inColltTaskInfoVo.setIsTaskDistr(BaseConstant.YES_Y);
        //  更新最后修改人id
        inColltTaskInfoVo.setLastUpdateUser(inColltTaskInfoVo.getLoginUserId());
        // 更新催收员id、名称
        inColltTaskInfoVo.setInputUserCode(inColltTaskInfoVo.getLoginUserId());
        UserInfo userIn = getUserInfo();
        inColltTaskInfoVo.setInputUserName(userIn.getUserName());
        int outputVo = colltTaskInfoService.updateByPk(inColltTaskInfoVo);
        return getResponseData(outputVo);
    }

    /**
     * 根据分配批次号和分配机构编号查询信息
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/collt/task/infos/export")
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> exportColltTaskInfo(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = colltTaskInfoService.queryAllByCondition(vo);
        riskmUtilService.displayOrgName(outputVo,"opOrgCode","opOrgName");
        riskmUtilService.displayUserName(outputVo,"opUserCode","opUserName");
        return getResponseData(outputVo);
    }

    /**
     * 通过条件查询，展示外催机构的催收任务数据
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/qry/collt/task/infos", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> qryColltTaskDistr(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = colltTaskInfoService.queryByConditionPage(vo);
        return getResponseData(outputVo);
    }

    /**
     * 任务状态为“"正常","延案"，获取延案催收任务数据、导入催收结果数据
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/qry/collt/task/infos/outs", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> qryColltTasksDelay(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = colltTaskInfoService.getOutsPopColltTasks(vo);
        return getResponseData(outputVo);
    }

    /**
     * 查询逾期超过180天的数据
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/acc/loan/overdue", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<ColltTaskInfoVO>> queryInfosOverdue(@RequestBody ColltTaskInfoVO vo) {
        List<ColltTaskInfoVO> outputVo = null;
        if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
            logger.info("开始导出数据！");
            outputVo = colltTaskInfoService.queryExInfosOverdue(vo);
            /*
             *
             * TODO 调用互金接口获取数据
             *
             * ReqAccountingPageinfoBean reqAccountingPageinfoBean = new ReqAccountingPageinfoBean();
             * reqAccountingPageinfoBean.setAcctSeqNo(vo.getColltTaskNo());
             * reqAccountingPageinfoBean.setBaseAcctNo(vo.getColltTaskNo());
             * reqAccountingPageinfoBean.setWriteOffReason(vo.getColltTaskNo());
             * reqAccountingPageinfoBean.setPageSize(vo.getSize());
             * reqAccountingPageinfoBean.setPageNum(vo.getPage());
             * reqAccountingPageinfoBean.setTotal(vo.getTotal());
             *
             * RespAccountingPageinfoBean outputVo = new RespAccountingPageinfoBean();
             * outputVo = AccountingPageinfoService.getAccountingPageinfo(reqAccountingPageinfoBean);
             *
             * */
        } else {
            logger.info("开始查询数据！");
            outputVo = colltTaskInfoService.queryInfosOverdue(vo);
        }
        addElements(outputVo);
        return getResponseData(outputVo);
    }

    private void addElements(List<ColltTaskInfoVO> outputVo) {
        outputVo.forEach(item -> {
            AccLoanVO accLoanVo = new AccLoanVO();
            accLoanVo.setBillNo(item.getLoanNo());
            AccLoanVO queryRsAccLoanVo = Optional.ofNullable(accLoanService.queryByPk(accLoanVo)).orElse(new AccLoanVO());
            item.setCertType(queryRsAccLoanVo.getCertType());
            item.setCertCode(queryRsAccLoanVo.getCertCode());
            item.setContNo(queryRsAccLoanVo.getContNo());
            item.setChannelCode(queryRsAccLoanVo.getChannelCode());
            item.setPrdName(queryRsAccLoanVo.getPrdName());
            item.setLoanAmount(queryRsAccLoanVo.getLoanAmount());
            item.setLoanStartDate(queryRsAccLoanVo.getLoanStartDate());
            item.setLoanEndDate(queryRsAccLoanVo.getLoanEndDate());
            item.setAccountStatus(queryRsAccLoanVo.getAccountStatus());
        });
    }

    /**
     * 撤回功能
     * @param vo
     * @return
     */
    @RequestMapping(value = "/collt/task/infos/withdraw", method = RequestMethod.POST)
    public @ResponseBody ResponseData<Integer> colltTasksWithdraw(@RequestBody ColltTaskInfoVO vo) {
        String colltTaskNo = vo.getColltTaskNo();
        logger.info("对催收任务进行撤回操作开始，催收任务编号为："+colltTaskNo);
        try {
            if(RiskColltConstant.STD_COLLT_WAY_01.equals(vo.getColltWay()) || RiskColltConstant.STD_COLLT_WAY_02.equals(vo.getColltWay())){
                //处于人工流转中
                if(RiskColltConstant.COLLT_TASK_STATUS_04.equals(vo.getTaskSts())){
                    riskmUtilService.dealTaskFlowApprIng(colltTaskNo);
                }
                //处于短信通知中
                if(RiskColltConstant.COLLT_TASK_STATUS_05.equals(vo.getTaskSts())){
                    riskmUtilService.dealTaskSmsApprIng(colltTaskNo);
                }
            }
            else{
                //处于延案中（委外）
                if(RiskColltConstant.STD_COLLT_WAY_03.equals(vo.getColltWay()) && RiskColltConstant.COLLT_OUTS_TASK_STS_06.equals(vo.getTaskSts())){
                    riskmUtilService.dealTaskDelayApprIng(vo);
                }
                //处于人工流转中(委外、诉讼)
                if(!RiskColltConstant.STD_COLLT_WAY_05.equals(vo.getColltWay()) && RiskColltConstant.COLLT_OUTS_TASK_STS_05.equals(vo.getTaskSts())){
                    riskmUtilService.dealTaskFlowApprIng(colltTaskNo);
                }
            }
            //恢复催收任务信息
            excuteTaskWithdraw(colltTaskNo, vo.getLoanNo());
        } catch (Exception e){
            e.printStackTrace();
            return ResponseData.create(-1, ApplicationConstance.FAILURE, e.getMessage(), false);
        }
        return getResponseData(1);
    }

    private void excuteTaskWithdraw(String colltTaskNo, String billNo) throws Exception {
        ColltTaskInfoVO updVo = new ColltTaskInfoVO();
        setUserInfoToVO(updVo);
        //催收余额 = 拖欠本金
        updVo.setColltBalance(colltTaskCommonService.getUnpdPrinBalByBillNo(billNo));
        updVo.setColltTaskNo(colltTaskNo);
        updVo.setLastUpdateUser(updVo.getLoginUserId());
        int updNum = colltTaskInfoService.rebackVOToOrign(updVo);
        if(updNum != 1){
            throw new Exception("通过催收任务编号:" + colltTaskNo + "，更新催收任务信息失败！");
        }
    }

    /**
     * 获取还款金额
     * 目前只有催收任务状态为"到期结案"时使用
     * @return
     */
    @PostMapping(value="/get/repay/lmt")
    public @ResponseBody ResponseData<Double> getRepayLmt(@RequestBody ColltTaskInfoVO vo) {
        BigDecimal result = BigDecimal.ZERO;
        LoanRepayDetailVO loanRepayDetailVO = new LoanRepayDetailVO();
        loanRepayDetailVO.setLoanNo(vo.getLoanNo());
        List<LoanRepayDetailVO> loanRepayDetailVOList = loanRepayDetailService.queryAllByLoanNo(loanRepayDetailVO);
        if(Objects.nonNull(loanRepayDetailVOList)){
            try {
                /**
                 * 根据业务规则：
                 *    委外催收一、二手到期日是60天，三手、长账龄到期日是90天，诉讼催收到期日是360天，风险代理到期日是720天。（计算开始的日期是委外催收分派审批通过的日期）
                 */
                String strDate = getAprvPassDate(vo.getColltBatchNo());

                String colltWay = vo.getColltWay();
                String outsHdleType = vo.getOutsHdleType();
                if(RiskColltConstant.STD_COLLT_WAY_03.equals(colltWay)){
                    if(RiskColltConstant.STD_HANDLE_TYPE_01.equals(outsHdleType) || RiskColltConstant.STD_HANDLE_TYPE_02.equals(outsHdleType)){
                        result = getRepayTotal(loanRepayDetailVOList, strDate, 60);
                    }
                    else if(RiskColltConstant.STD_HANDLE_TYPE_03.equals(outsHdleType) || RiskColltConstant.STD_HANDLE_TYPE_04.equals(outsHdleType)){
                        result = getRepayTotal(loanRepayDetailVOList, strDate, 90);
                    }
                    else{
                        throw new Exception("委外催收新增经手类型，请检查是否是未新增业务处理规则");
                    }
                }
                else if(RiskColltConstant.STD_COLLT_WAY_04.equals(colltWay)){
                    result = getRepayTotal(loanRepayDetailVOList, strDate, 360);
                }
                else if(RiskColltConstant.STD_COLLT_WAY_05.equals(colltWay)){
                    result = getRepayTotal(loanRepayDetailVOList, strDate, 720);
                }
                else{
                    throw new Exception("新增外催方式，请检查是否是未新增业务处理规则");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseData.create(null, ApplicationConstance.FAILURE, e.getMessage(), false);
            }
        }
        return getResponseData(result.doubleValue());
    }

    private String getAprvPassDate(String colltBatchNo) throws Exception {
        //通过批次号获取催收任务的审批通过日期
        ColltTaskDistrAppVO colltTaskDistrAppVO = new ColltTaskDistrAppVO();
        colltTaskDistrAppVO.setColltBatchNo(colltBatchNo);
        colltTaskDistrAppVO = colltTaskDistrAppService.queryByCondition(colltTaskDistrAppVO);
        if(Objects.isNull(colltTaskDistrAppVO) || StringUtil.isNullorBank(colltTaskDistrAppVO.getAprvTime())){
            throw new Exception("************ 通过批次号:" + colltBatchNo + "，获取催收任务的分配日期失败");
        }
        return colltTaskDistrAppVO.getAprvTime().substring(0, 10);
    }

    /**
     *
     * @param loanRepayDetailVOS 还款明细集
     * @param strDate 起始日期
     * @param days 期限天数
     * @return
     */
    private BigDecimal getRepayTotal(List<LoanRepayDetailVO> loanRepayDetailVOS, String strDate, int days) {
        return loanRepayDetailVOS.stream()
                .filter(loanRepayDetailVO -> {
                    return TimeUtil.compareDate(loanRepayDetailVO.getSetlValDt(), strDate) != -1 &&
                            TimeUtil.compareDate(loanRepayDetailVO.getSetlValDt(), DateUtility.ADD_DAY(strDate, days)) == -1;
                })
                .map(LoanRepayDetailVO::getTotalAmt)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }


    //20201119lmj根据催收编号查询催收任务信息
    @PostMapping(value = "/get/task/byno")
    public @ResponseBody ResponseData<ColltTaskInfoVO> getColltTaskInfoVOByTaskNo(@RequestBody ColltTaskInfoVO taskVO){
        ColltTaskInfoVO resVO = colltTaskInfoService.getColltTaskInfoVOByTaskNo(taskVO);
        return getResponseData(resVO);
    }

}
